﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    [CreateAssetMenu(fileName = "AudioGroup", menuName = "Nirvana/Audio/AudioGroup")]
    public class AudioGroup : ScriptableObject
    {
        private enum EliminationMode
        {
            [EnumLabel("Skip Current Audio")]
            Skip,
            [EnumLabel("Stop By Left Time")]
            Stop
        }

        [EnumLabel]
        [SerializeField]
        [Tooltip("The elimination mode of this group.")]
        private EliminationMode eliminationMode;

        [Tooltip("The play interval between two play.")]
        [SerializeField]
        private float interval;

        [SerializeField]
        [Tooltip("The max count of this item can play, 0 mean no limit.")]
        private int maxCount;

        private HashSet<IPlayerAudio> audioplayers = new HashSet<IPlayerAudio>();
        private float lefttime;
        private int playcount;
        public int PlayingCount { get { return this.playcount; } }
        private bool IsPlayEnd()
        {
            return Time.realtimeSinceStartup >= this.lefttime + this.interval
                && (this.maxCount <= 0 || this.playcount < this.maxCount);
        }

#if UNITY_EDITOR
        [InitializeOnLoadMethod]
        private static void Init()
        {
            EditorApplication.playModeStateChanged += OnInit;
        }

        private static void OnInit(PlayModeStateChange state)
        {
            string[] AudioGroups = AssetDatabase.FindAssets("t:AudioGroup");
            foreach (var p in AudioGroups)
            {
                string path = AssetDatabase.GUIDToAssetPath(p);
                AudioGroup audioGroup = AssetDatabase.LoadAssetAtPath<AudioGroup>(path);
                audioGroup.lefttime = 0f;
                audioGroup.playcount = 0;
            }
        }
#endif
        public void Add(IPlayerAudio audio)
        {
            this.playcount++;
            this.lefttime = Time.realtimeSinceStartup;
            if(this.eliminationMode != EliminationMode.Skip)
            {
                this.audioplayers.Add(audio);
            }
        }

        public void Remove(IPlayerAudio audio)
        {
            this.playcount--;
            if (this.eliminationMode != EliminationMode.Skip)
            {
                this.audioplayers.Remove(audio);
            }
        }

        public bool IsStop()
        {
            if (this.IsPlayEnd()) return true;
            if (this.eliminationMode == EliminationMode.Skip) return false;
            if (this.audioplayers.Count == 0) return false;
            IPlayerAudio audio = null;
            float maxVelome = float.MaxValue;
            foreach(var p in this.audioplayers)
            {
                if(p.IsPlaying && p.GetRamingTime() >= 0 && p.GetRamingTime()< maxVelome)
                {
                    maxVelome = p.GetRamingTime();
                    audio = p;
                }
            }

            if(audio != null)
            {
                audio.Stop();
                return true;
            }
            return false;

        }



    }
}

